'''
Library Class - 图书馆管理类

类的职责：
1. 管理图书馆的所有图书信息
2. 提供图书的增删改查功能
3. 处理图书借阅和归还业务
4. 维护借阅记录

数据结构：
1. books_: Dict[int, Book]    // 存储所有图书，key为ID，value为Book对象
2. records_: Dict[int, str]   // 存储借阅记录，key为图书ID，value为借阅人姓名

核心方法：
1. 图书信息管理：
   - addBook(book: Book) -> bool                                        // 添加新图书，ID不可重复
   - removeBook(bookID: int) -> bool                                    // 删除图书，已借出不可删除
   - updateBookInfo(ID: int, name: str, author: str, publisher: str) -> bool  // 更新图书信息
   - getAllBooks() -> List[Book]                                        // 获取所有图书列表

2. 图书查询：
   - queryBookByID(id: int) -> Book                                    // 精确查找指定ID的图书
   - queryBookByName(name: str) -> List[Book]                          // 模糊查找包含指定书名的图书
   - queryBookByAuthor(author: str) -> List[Book]                      // 查找指定作者的所有图书

3. 借阅管理：
   - rentBook(ID: int, userName: str) -> bool                          // 借阅图书，检查存在性和可借状态
   - returnBook(ID: int, userName: str) -> bool                        // 归还图书，验证借阅人身份

异常处理：
1. 图书操作：查重、存在性检查
2. 借阅操作：状态检查、身份验证
3. 所有操作结果通过返回值反馈

使用示例：
    library = Library()
    book = Book(1001, "Python编程", "作者", "出版社")
    library.addBook(book)
    library.rentBook(1001, "张三")
    library.returnBook(1001, "张三")
'''

from book import Book


class Library:

    def __init__(self):
        self.books_ = {}
        self.records_ = {}

        # 添加一些初始数据用于测试
        self.addBook(Book(1001, "Python编程：从入门到实践", "埃里克·马瑟斯", "人民邮电出版社"))
        self.addBook(Book(1002, "三体", "刘慈欣", "重庆出版社"))
        self.addBook(Book(1003, "流畅的Python", "Luciano Ramalho", "人民邮电出版社"))
        self.addBook(Book(1004, "围城", "钱钟书", "人民文学出版社"))

        return
    
    def addBook(self,book) -> bool:
        if (book.ID_  in self.books_) == False:
            self.books_[book.ID_] = book
            return True
        return False
    
    def removeBook(self,ID: int) -> bool:
        if ID in self.books_ and self.books_[ID].isRented_ == False:
            del self.books_[ID]
            return True
        return False
    
    def queryBookByName(self,name: str) -> list:
        result = []
        for book in self.books_.values():
            if name in book.name_:
                result.append(book)
        
        return result

    def queryBookByAuthor(self,author: str) -> list:
        result = []
        for book in self.books_.values():
            if author == book.author_:
                result.append(book)
        
        return result
    
    def queryByNameAndAuthor(self,name: str,author: str) -> list:
        result = []
        temp_book = self.queryBookByName(name)

        for book in temp_book:
            if book.author_ == author:
                result.append(book)

        return result

    def queryBookByID(self,id: int) -> Book:
        for book in self.books_.values():
            if(book.ID_ == id):
                return book
        return None

    
    def updateBookInfo(self,ID: int,name: str,author: str,publisher: str) -> bool:
        if ID in self.books_:
            book = self.books_[ID]
            book.name_ = name
            book.author_ = author
            book.publisher_ = publisher
            return True
        return False
    
    def rentBook(self,ID: int,userName: str) -> bool:
        if ID in self.books_:
            book = self.books_[ID]
            if book.isRented_ == True:
                return False
            book.isRented_ = True
            book.num_of_times_rented_ += 1
            self.records_[ID] = userName

            return True
        
        return False
    
    def returnBook(self,ID: int,userName: str) -> bool:
        if ID in self.books_ and ID in self.records_ and self.records_[ID] == userName:
            book = self.books_[ID]
            book.isRented_ = False
            del self.records_[ID]
            return True
        return False
    
    def getAllBooks(self) -> list:
        return list(self.books_.values())
    
